package com.diorsunion.hedge.client.io.netty;

import com.diorsunion.hedge.io.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.stereotype.Component;

/**
 * Created by harley-dog on 2016/5/17.
 */
@Component
public class NettyClientHandler extends SimpleChannelInboundHandler<Message>{
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            switch (e.state()) {
                case WRITER_IDLE:
                    Message message = Message.genHeartBeatMessage();
                    ctx.writeAndFlush(message);
                    System.out.println("send ping to server----------");
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, Message msg) throws Exception {
        System.out.println(msg);
    }
}
